package ch09_异常与处理;

class EmptyFieldException extends Exception{
	public EmptyFieldException(String which) {
		super(which+"不能为空！");
	}
}
class InvalidUserException extends Exception{
	public InvalidUserException() {
		super("用户名或密码错误！");
	}
}
class ForbiddenUserException extends Exception{

	public ForbiddenUserException() {
		super("用户已被禁用！");
	}
}
class LoginService{
	public void login(String username,String password)throws EmptyFieldException,ForbiddenUserException,InvalidUserException{
		if(username ==null|| username.trim().length()<1) {
			throw new EmptyFieldException("用户名");
		}
		if(password ==null|| password.length()<1) {
			throw new EmptyFieldException("密码");
		}
		if(username.trim().equals("admin")&& password.equals("admin")) {
			boolean b = System.currentTimeMillis()%2==0;
			if(b) {
				throw new ForbiddenUserException();
			}
		}else {
			throw new InvalidUserException();
		}
	}
}
public class LoginUI {
	public static void main(String[] args) {
		LoginService loginService = new LoginService();
		try {
			loginService.login("admin","admin");
			System.out.println("登录成功！");
		}catch(EmptyFieldException| InvalidUserException| ForbiddenUserException e) {
			System.err.println(e.getMessage());
		}
	}
}
